package servlet;

import dao.GoodsDao;
import dao.TypeDao;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;
import java.io.IOException;
import java.util.Collection;
import java.util.Iterator;

@WebServlet("/addType")
@MultipartConfig
public class GoodsTypePrepareAddServlet extends HttpServlet {

    private static final long serialVersionUID = 2L;

    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        String typeName = request.getParameter("addGoodTypeName");
        String typeDesc = request.getParameter("addGoodTypeDesc");

        TypeDao dao = new TypeDao();
        int retSureTypeName = dao.sureType(typeName);
        if(retSureTypeName >= 1){
            //用户名已存在
            request.getSession().setAttribute("errorMsg", "商品类型已存在");
            request.getRequestDispatcher("./typeList").forward(request, response);
            return;
        }
        int ret = dao.addType(typeName,typeDesc);
        if(ret == -1) {
            request.getRequestDispatcher("error.jsp").forward(request, response);
            return;
        }else {
            request.getSession().setAttribute("successMsg", "增添成功！");
            request.getRequestDispatcher("./typeList").forward(request, response);
        }
    }
}